package ist.utl.pt.smartfleet.stationterminal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import ist.utl.pt.smartfleet.stationterminal.Protocol.Charge;
import ist.utl.pt.smartfleet.stationterminal.Protocol.Msg;
import ist.utl.pt.smartfleet.stationterminal.Protocol.Station;
import ist.utl.pt.smartfleet.stationterminal.Protocol.Traveller;
import ist.utl.pt.smartfleet.stationterminal.Protocol.Trip;
import ist.utl.pt.smartfleet.stationterminal.Protocol.location;
import ist.utl.pt.smartfleet.stationterminal.rest.webservice.station.StationWebServiceUtil;
import ist.utl.pt.smartfleet.stationterminal.rest.webservice.traveller.TravellerWebServiceUtil;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

	/*	Possible Vehicle states:
	 *   
	 *  - atStation
	 *  - atCharging
	 *  - Tripin
	 */

public class SimService extends Service{

	private Socket SockSimulator;
	
	private PrintWriter OutSimulator;
	
	private BufferedReader InSimulator;
	
	private int _id;
	
	private Location stationLocation;
	
	//Base de dados
	private BDAdapter mDbHelper;	
	
	private String _address;

	private Messenger main;

	private NetServer netServer;
	
	RegisterValues reg;
	
	private SimThread simThread = new SimThread();;
	
	final Messenger mMessenger = new Messenger(new IncomingHandler());
	
	static final int Register_Station = 1;
	static final int Unregister_Station = 2;
	
	public static final int trip_msg = 1;
	public static final int arrival_msg = 2;
	public static final int charge_msg = 3;
	public static final int trip_search = 4;
	
	private ArrayList<Integer> vehiclesCharging = new ArrayList<Integer>();
	private ArrayList<Integer> vehiclesWaitingCharge = new ArrayList<Integer>();
	
	class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case Register_Station:
            	reg = (RegisterValues) msg.obj;
        		main = msg.replyTo;
        		_id = msg.arg1;     
        		simThread.setAddress(reg.get_sim_addr());
        		simThread.setPort(reg.get_sim_port());
        		simThread.start();
        	//	Register(msg.arg1, (String) msg.obj);
        		//Register(_id, reg.get_netport(), reg.get_emulport(), reg.getLongitude(), reg.getLatitude());
            		break;
            case Unregister_Station:
            	Unregister(msg.arg1);
        		break;
            case trip_search:
            	tripSearch();
        		break;
                default:
                    super.handleMessage(msg);
            }
        }
    }
	

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
    	//Toast.makeText(this, "Simlulator service started", Toast.LENGTH_SHORT).show();
        //return myBinder;
    	return mMessenger.getBinder();
    }
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        
        netServer = new NetServer(10000);
        netServer.start();
        
        /** Create database **/
        
        mDbHelper = new BDAdapter(this);
        
	

	
    }
    
    @Override
    public void onDestroy() {
        
    	netServer.close();
    	//netServer.stop();
    	
        try {
        	
			SockSimulator.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }

    private void Register(int id, int netport, int emulport, double lon, double lat){
    	
    	OutSimulator.println("RegisterStation "+id+" "+netport+" "+emulport+" "+lon+" "+lat);
    	
    }
    
    private void Unregister(int id){
    	
    	OutSimulator.println("UnregisterStation "+id);
    }
    
    private void sendCharge(int bateria, String address, int port){
    	
    	try {
    		
    		Socket vehicleSock = new Socket(address, port);
    		
    		OutputStream nave = vehicleSock.getOutputStream();
    		
    		Msg.Builder msg = Msg.newBuilder();
    		msg.setId(charge_msg);
    		Charge.Builder charge = Charge.newBuilder();
    		charge.setLimit(bateria);
    		msg.setCharge(charge);
    		msg.build().writeTo(nave);
    		vehicleSock.close();
    	
    	} catch (UnknownHostException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    private void sendTrip(ArrayList<String> passageiros, int idNave, Location loc, int idStation, String stationName){
    	
    	try {
    		mDbHelper.open(_id);
    		
    		String address = mDbHelper.getVehicleAddr(idNave);
    		
    		int port = mDbHelper.getVehiclePort(idNave);
    		String idP;
    		String idL;
    		String out;
    		String name;
    		String morada;
    		
    		
    		Socket vehicleSock = new Socket(address, port);
    		
    		System.out.println("SEND TRIP ADRESS : "+address);
    		System.out.println("SEND TRIP PORT : "+port);
    		
    		OutputStream nave = vehicleSock.getOutputStream();
    		
    		Msg.Builder msg = Msg.newBuilder();
    		msg.setId(trip_msg);
    		Trip.Builder trip = Trip.newBuilder();
    	//	trip.setVehicle(idNave);
    		
    		
    		for( int i=0; i < passageiros.size(); i++){
    			Traveller.Builder trav = Traveller.newBuilder();
    			location.Builder locA = location.newBuilder();
    			idP = passageiros.get(i);
    			mDbHelper.setTravellerState(idP, "atTrip");
    			idL = mDbHelper.getTravellerLocation(idP);
    			locA.setLat(mDbHelper.getLocationAdress(idL).getLatitude());
    			locA.setLon(mDbHelper.getLocationAdress(idL).getLongitude());
    			morada = mDbHelper.getLocationName(idL);
    			locA.setName(morada);
    			
    			String x = mDbHelper.getStationLocation(_id);
    			location.Builder locS = location.newBuilder();
    			locS.setLat(stationLocation.getLatitude());
    			locS.setLon(stationLocation.getLongitude());
    			locS.setName(mDbHelper.getLocationName(x));
    			
    			name = mDbHelper.getTravellerName(idP);
    			trav.setState("trippin");
    			trav.setName(name);
    			trav.setSize(mDbHelper.getTravellerSize(idP));
    			trav.setCheckin(mDbHelper.getTravellerTime(idP));
    			trav.setDestination(locA);
    			trav.setId(Integer.parseInt(idP));
    			
    			Station.Builder sta = Station.newBuilder();
    			sta.setLocation(locS);
    			sta.setId(_id);
    			
    			trav.setStation(sta);
    			
    			trip.addTravellers(trav);
    			
    			//meter a estação destino VER PROTO
    			out = "Passageiro: "+name+" embarque no veiculo :"+idNave+" Com destino a "+morada;
    			
    		//	TravellerWebServiceUtil.updateState(idP, "atTrip");
    			
    			
    			try {
        			main.send(Message.obtain(null, StationTerminal.boardingPassagers, out));
    			} catch (NumberFormatException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (RemoteException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}    			
    		}
    		

			location.Builder locF = location.newBuilder();
			locF.setLat(loc.getLatitude());
			locF.setLon(loc.getLongitude());
			locF.setName(stationName);
			
			Station.Builder stationF = Station.newBuilder();
			stationF.setId(idStation);
			stationF.setLocation(locF);
			
			trip.setLastDestination(stationF);
    		
    		mDbHelper.setVheicleState(idNave, "Tripin");
    		
    		mDbHelper.pause();
    		msg.setTrip(trip);
    		msg.build().writeTo(nave);
    		vehicleSock.close();
    		
    		System.out.println("TRIP SEND to Vehicle: "+ idNave);
    	
    	} catch (UnknownHostException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    }
    
    public void tripSearch(){
		ArrayList<String> PassList = null;
		ArrayList<Integer> idNaves = new ArrayList<Integer>();
		ArrayList<Location> locais = new ArrayList<Location>();
		
		double somaDistancias = 0;
		String idL;
		int idNave = 0;
		Location loc;
		
		//vai à bd de passageiros buscar os n travellers mais antigos onde o nr de passgeiros delas tdas somadas < = 4
		mDbHelper.open(_id);
				
		PassList = mDbHelper.getTravellerGroup();
		mDbHelper.pause();
		
		if (PassList.size()==0){
			System.out.println("LISTA DA PASSGEIROS VAZIA");
			return;} 
		else {
		
		//ordenar lista
		PassList = ordenaListaPassageiros(PassList);
		System.out.println("Passei a lista ordenada");
		printArrayString(PassList);
		
		mDbHelper.open(_id);
		Location destinoLonge = null;
		//preenche a lista de Locais da trip
		for (int i=0; i < PassList.size(); i++){
			idL = mDbHelper.getTravellerLocation(PassList.get(i));
			loc = mDbHelper.getLocationAdress(idL);
			locais.add(i, loc);
			if (i+1 == PassList.size()){
				destinoLonge = loc;
			}
		}
		
		//IR BUSCAR A ESTAÇÂO MAIS PERTO da ultimo destino  COM WEB SERVICE
		List<ist.utl.pt.smartfleet.stationterminal.station.Station> stations = StationWebServiceUtil.getOnLineStations();
		printArrayList(stations);
		int idaux = 0;
		ist.utl.pt.smartfleet.stationterminal.station.Station stationA;
		Location aux = new Location("teste");
		Location fin = new Location("final");
		String estacao = null;
		double distanciaMinima = 5000;
		double daux;
		
		for (int i = 0 ; i < stations.size() ; i++) {
			stationA = stations.get(i);
			aux.setLatitude(stationA.getLocation().getLatitude());
			aux.setLongitude(stationA.getLocation().getLongitude());
			daux = getDistanceBetween(aux, destinoLonge);
			if(daux < distanciaMinima){
				distanciaMinima = daux;
				idaux = stations.get(i).getIdStation();
				fin.setLatitude(stations.get(i).getLocation().getLatitude());
				fin.setLongitude(stations.get(i).getLocation().getLongitude());
				estacao = stations.get(i).getLocation().getName();
			}
		} 
		
		
		//soma a distancia total dos travelers
		somaDistancias = 0;
		somaDistancias += getDistanceBetween(locais.get(0), stationLocation);
		if (locais.size() != 1) {
			for (int i=1; i < locais.size(); i++){
				somaDistancias += getDistanceBetween(locais.get(i-1), locais.get(i));
			}
		}
		
		//procura veiculos onStation e estima a distancia que eles aguentam
		idNaves = mDbHelper.getAvailableVehicle();
		
		System.out.println("IDs da nave DA NAVE ESCOLHIDA ");
		printArrayInt(idNaves);
		
	/*	if(idNaves.size() == 0){
			//mDbHelper.pause();
			return;
		} */
		
		for (int i=0; i < idNaves.size(); i++){
			if (mDbHelper.getVehicleBattery(idNaves.get(i)) > somaDistancias){
				idNave = idNaves.get(i); //escolhe um veiculo para a trip
				break;
			}
		}
		
		System.out.println("ID da nave DA NAVE ESCOLHIDA "+idNave);
		mDbHelper.pause();
		sendTrip(PassList, idNave, fin, idaux, estacao);
		
			}
		
	}
    
    
    
    
    public boolean canVehicleCharge(int id){
		if(this.vehiclesCharging.size() <= 1 && !this.vehiclesCharging.contains(id)){
		this.vehiclesCharging.add(id);
		return true;
		} else return false;
	}
   
	public boolean rmVehicleCharging(int id){
		if(this.vehiclesCharging.contains(id)){
			vehiclesCharging.remove((Object)id);
			return true;
		} else return false;
		
	}
	
	public boolean isVehicleCharging(int id){
		if(this.vehiclesCharging.contains(id))
				return true;
		else return false;
	}
	

    
    public void rmVehiclesWaitingCharge(int id){
		if(this.vehiclesWaitingCharge.contains(id))
			vehiclesWaitingCharge.remove((Object)id);
		
	}
    
    public void addVehiclesWaitingCharge(int id){
		if(!this.vehiclesWaitingCharge.contains(id))
		this.vehiclesWaitingCharge.add(id);
	}
    
    public boolean areVehiclesWaitingCharge(){
		if(this.vehiclesWaitingCharge.size() != 0)
			return true;
		else return false;
	}
    
    public int getVehicleWaiting(){
		if(this.vehiclesWaitingCharge.size() != 0)
			return vehiclesWaitingCharge.get(0);
		return 0;
	}
    
    public double getDistanceBetween(Location loc1, Location loc2){
		
		double R = 6371; // raio da terra em km
		double dLat = Math.toRadians(loc1.getLatitude()-loc2.getLatitude());
		double dLon = Math.toRadians(loc1.getLongitude()-loc2.getLongitude());
		double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
		        Math.cos(loc1.getLatitude()*(Math.PI/180)) * Math.cos(loc2.getLatitude()*(Math.PI/180)) * 
		        Math.sin(dLon/2) * Math.sin(dLon/2); 
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		return R * c;
		
	}
	
	public ArrayList<String> ordenaListaPassageiros(ArrayList<String> idPs){
		int i;
		String idL;
		String idaux = null;
		Location loc;
		Location fixa = null;
		String stL;
		double distanciaMinima = 5000;
		double daux = 5000;
		ArrayList<String> res = new ArrayList<String>();
		
		mDbHelper.open(_id);
		
		stL = mDbHelper.getStationLocation(_id);
		stationLocation = mDbHelper.getLocationAdress(stL);
		switch (idPs.size()) {
		case 1:
			res = idPs;
			break;
		case 2:
			distanciaMinima = 5000;
			for (i=0; i < idPs.size(); i++){
				idL = mDbHelper.getTravellerLocation(idPs.get(i));
				loc = mDbHelper.getLocationAdress(idL);
				daux = getDistanceBetween(stationLocation, loc);
				if(daux < distanciaMinima){
					distanciaMinima = daux;
					idaux = idPs.get(i);
				}
			}
			res.add(0, idaux); //adiciona 1ª morada mais perto da estação à lista de resultados
			idPs.remove(idaux);
			res.add(1, idPs.get(0));
			break;
		case 3:
			distanciaMinima = 5000;
			for (i=0; i < idPs.size(); i++){
				idL = mDbHelper.getTravellerLocation(idPs.get(i));
				loc = mDbHelper.getLocationAdress(idL);
				daux = getDistanceBetween(stationLocation, loc);
				if(daux < distanciaMinima){
					distanciaMinima = daux;
					idaux = idPs.get(i);
					fixa = loc;
				}
			}
			res.add(0, idaux); //adiciona 1ª morada mais perto da estação à lista de resultados
			idPs.remove(idaux);
			distanciaMinima = 5000;
			for (i=0; i < idPs.size(); i++){
				idL = mDbHelper.getTravellerLocation(idPs.get(i));
				loc = mDbHelper.getLocationAdress(idL);
				daux = getDistanceBetween(fixa, loc);
				if(daux < distanciaMinima){
					distanciaMinima = daux;
					idaux = idPs.get(i);
					fixa = loc;
				}
			}
			res.add(1, idaux); //adiciona 2ª morada mais perto da 1ª morada
			idPs.remove(idaux);
			res.add(2, idPs.get(0));
			break;
		case 4:
			distanciaMinima = 5000;
			for (i=0; i < idPs.size(); i++){
				idL = mDbHelper.getTravellerLocation(idPs.get(i));
				loc = mDbHelper.getLocationAdress(idL);
				daux = getDistanceBetween(stationLocation, loc);
				if(daux < distanciaMinima){
					distanciaMinima = daux;
					idaux = idPs.get(i);
					fixa = loc;
				}
			}
			res.add(0, idaux); //adiciona 1ª morada mais perto da estação à lista de resultados
			idPs.remove(idaux);
			
			distanciaMinima = 5000;
			for (i=0; i < idPs.size(); i++){
				idL = mDbHelper.getTravellerLocation(idPs.get(i));
				loc = mDbHelper.getLocationAdress(idL);
				daux = getDistanceBetween(fixa, loc);
				if(daux < distanciaMinima){
					distanciaMinima = daux;
					idaux = idPs.get(i);
					fixa = loc;
				}
			}
			res.add(1, idaux); //adiciona 2ª morada mais perto da 1ª morada
			idPs.remove(idaux);
			
			distanciaMinima = 5000;
			for (i=0; i < idPs.size(); i++){
				idL = mDbHelper.getTravellerLocation(idPs.get(i));
				loc = mDbHelper.getLocationAdress(idL);
				daux = getDistanceBetween(fixa, loc);
				if(daux < distanciaMinima){
					distanciaMinima = daux;
					idaux = idPs.get(i);
					fixa = loc;
				}
			}
			res.add(2, idaux); //adiciona 3ª morada mais perto da 1ª morada
			idPs.remove(idaux);
			res.add(3, idPs.get(0));
			break;
		}
		mDbHelper.pause();
		return res;
	}
    
    public void printArrayString(ArrayList<String> idPassengers){
    	System.out.println("ARRAY PRINT ____INIT");
    	for (int i = 0 ; i < idPassengers.size(); i++)
    		System.out.println(idPassengers.get(i));
    	System.out.println("ARRAY PRINT ____FIM");
    }
    
    public void printArrayList(List<ist.utl.pt.smartfleet.stationterminal.station.Station> stations){
    	System.out.println("ARRAY PRINT ____INIT");
    	for (int i = 0 ; i < stations.size(); i++)
    		System.out.println(stations.get(i).getIdStation());
    	System.out.println("ARRAY PRINT ____FIM");
    }
    
    public void printArrayInt(ArrayList<Integer> idPassengers){
    	System.out.println("ARRAY PRINT ____INIT");
    	for (int i = 0 ; i < idPassengers.size(); i++)
    		System.out.println(idPassengers.get(i));
    	System.out.println("ARRAY PRINT ____FIM");
    }
    
    public void printArrayLocais(ArrayList<Location> idPassengers){
    	System.out.println("ARRAY Locations PRINT ____INIT");
    	for (int i = 0 ; i < idPassengers.size(); i++){
    		System.out.println(idPassengers.get(i).getLatitude());
    	System.out.println(idPassengers.get(i).getLongitude()); }
    	System.out.println("ARRAY PRINT ____FIM");
    }
    
    public String transformAddress(String adr){
    	if( adr.compareTo("127.0.0.1") == 0){
    		adr = reg.get_sim_addr();   //MUDAR PO SIM ADDR receber sim adr por msg
    	} else if (adr.equals(_address))
    		adr = "10.0.2.2";
    	return adr;
    }
    
    public void addVehicleCharging(int id){
    	  if(!this.vehiclesCharging.contains(id))
    	   vehiclesCharging.add(id);
    	 }
    
    /** Class netServer **/
    
    class NetServer extends Thread{

    	private ServerSocket socket;
    	private boolean state = true;
    	//private Messenger main;
    	
    	
    	public void close(){
    		
    		try {
				this.socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("socket Closed");
    	}
    	
    	public NetServer(int port) {
        	super("NetServer");
        	
        	//this.main = a;
        	
        	try {
        		
    			this.socket = new ServerSocket(port);
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	public void run() {
        	
    		try {
    			
    			while (state)
    			
    				new NetServerHandler(this.socket.accept()).start();
    			
    			 
    		} catch (IOException e) {
    			System.out.println("ERRO");
    			e.printStackTrace();
    		}
    	
    	}
    	
    	
    	/** Class netServer Handler **/
        
        class NetServerHandler extends Thread{

        	private Socket socket = null;
        	
        	//private Messenger main;

            public NetServerHandler(Socket socket) {
            	super("ClientThread");
            	
            	//this.main = a;
            	this.socket = socket;
            
            }
            
            public void run(){
    			
    			try {
    				InputStream in2 =  this.socket.getInputStream();
	        			
    				Msg message = Msg.parseFrom(in2);
    				
    				socket.close();
    				
    				switch(message.getId()){
	        			
    				case arrival_msg:
    					System.out.println("SimService arrival_msg chegou");
    					
    					boolean sendCharge = false;
    					int goCharge = 0;
    					
    					mDbHelper.open(_id);
    					
    					ArrayList<Integer> vehicles = mDbHelper.getVehicles();
    					
    					//parse da mensagem
    					int idV  = message.getArrival().getId();
    					String adrV = message.getArrival().getAddress(); //endereço da nave
    					int portV = message.getArrival().getPort(); //port da nave
    					int batV = message.getArrival().getBattery();
    					
    					System.out.println("id: "+idV);
    					System.out.println("bateria: "+batV);
    					System.out.println("address: "+ transformAddress(adrV));
    					System.out.println("port: " + portV);
    					
    					
    					if (vehicles.size() != 0 && vehicles.contains(idV)){  //VER SE O SEARCH EM BRANCO NAO ESTOIRA
    						if (mDbHelper.getVehicleState(idV).compareTo("atCharging") == 0 ){
    							mDbHelper.setVheicleState(idV, "atStation");
    							mDbHelper.setVheicleBattery(idV, batV);
    							rmVehicleCharging(idV);
    							if (areVehiclesWaitingCharge()){
    								sendCharge = true;
    								goCharge = getVehicleWaiting();
    								}
    						}
    						if (mDbHelper.getVehicleState(idV).compareTo("Tripin") == 0 ){
    							mDbHelper.setVheicleState(idV, "atStation");
    							if (batV <= 1600){
    								sendCharge = true;
    								goCharge = idV;
    							}
    						}   	
    					} else {
    							mDbHelper.createVehicle(idV, batV, "atStation", transformAddress(adrV), portV);
    							if(batV <= 1600){
    								sendCharge = true;
    								goCharge = idV;
    							}
    					}
    					if(sendCharge){ //goCharge precisa de carregar
    						if(canVehicleCharge(goCharge)){
    							System.out.println("SimService: veiculo nao tem bateria");
    							String ad = mDbHelper.getVehicleAddr(goCharge);
    							int p = mDbHelper.getVehiclePort(goCharge);
    							mDbHelper.setVheicleState(goCharge, "atCharging");
    							mDbHelper.pause();
    							sendCharge(36000, ad, p); //manda carregar
    							addVehicleCharging(goCharge);
    							rmVehiclesWaitingCharge(goCharge);
    						} else addVehiclesWaitingCharge(goCharge); //mete a nave em espera de lugar na carga
    					} 
    					mDbHelper.pause();	
    					tripSearch();
    					
    					break;
    				} 
	        			
	        			
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				System.out.println("SimService SOCKET foi com os porcos");
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
    
    
    class SimThread extends Thread{
    	
    	String address;
    	
    	int port;
    	
    	public SimThread(){
    		
    	}
    	
    	public void setPort(int p){
    		this.port = p;
    	}
    	
    	public void setAddress(String a){
    		this.address = a;
    	}
    	
    	public void run(){
    		
    		String inline;
			String[] split;
			try {
				
				SockSimulator = new Socket(address, port);
				OutSimulator = new PrintWriter(SockSimulator.getOutputStream(), true);
				InSimulator = new BufferedReader(new InputStreamReader(SockSimulator.getInputStream()));
				
				
				new Thread(){
				      
				      public void run(){
				       
				       try {
				        Thread.sleep(1000);
				       } catch (InterruptedException e) {
				        e.printStackTrace();
				       }
				       Register(_id, reg.get_netport(), reg.get_emulport(), reg.getLongitude(), reg.getLatitude());

				      }
				      
				     }.start();
				
				while ((inline = InSimulator.readLine()) != null){
					
					split = inline.split(" ");
					
					if(split[0].equals("Address")){
    					
    					_address = split[1];
    					
    				}

					
				}
				
			} catch (IOException e) {
				System.out.println("ERRO ao iniciar socket?");
				e.printStackTrace();
			}
    		
    	}
	}
	
}
